﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content;

namespace Jemgine.Engine
{
    public class DynamicGrid<T> where T : class, new()
    {
        public List<List<T>> _data;
        public int Width { get; set; }
        public int Height { get; set; }

        public DynamicGrid() 
        {
            _data = new List<List<T>>();
            Width = 0;
            Height = 0;
        }

        public DynamicGrid(int W, int H)
        {
            Init(W, H);
        }

        public void Init(int W, int H)
        {
            Width = W;
            Height = H;
            _data = new List<List<T>>();
            for (int X = 0; X < Width; ++X)
            {
                _data.Add(new List<T>());
                for (int Y = 0; Y < Height; ++Y)
                    _data[X].Add(new T());
            }
        }

        public T this[int X, int Y]
        {
            get { return _data[X][Y]; }
        }

        public void AddRow(int Where)
        {
            for (int i = 0; i < Width; ++i)
                _data[i].Insert(Where, new T());
            Height += 1;
        }

        public void RemoveRow(int Where)
        {
            for (int i = 0; i < Width; ++i)
                _data[i].RemoveAt(Where);
            Height -= 1;
        }

        public void AddColumn(int Where)
        {
            var NewCol = new List<T>();
            for (int Y = 0; Y < Height; ++Y) NewCol.Add(new T());
            _data.Insert(Where, NewCol);
            Width += 1;
        }               

        public void RemoveColumn(int Where)
        {
            _data.RemoveAt(Where);
            Width -= 1;
        }
    }
}
